package elaprendiz.ventanas.paneles;

import elaprendiz.controllers.CAlmacenProducto;
import elaprendiz.controllers.CKardex;
import elaprendiz.modelbd.AlmacenProducto;
import elaprendiz.modelbd.Kardex;
import elaprendiz.modelbd.Producto;
import elaprendiz.modelgui.ModeloTablaApertura;
import elaprendiz.util.CeldaSpinnerEditor;
import elaprendiz.util.Helper;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Calendar;
import javax.swing.JOptionPane;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;

/**
 *
 * @author El APRENDIZ www.elaprendiz.net63.net
 */
public class PanelAperturaDeInventario extends javax.swing.JPanel {

    /**
     * Creates new form PanelDetalleGrabarProducto
     */
    private Producto prd;
    private JOptionPane op;
    private boolean aceptado = false;
    private Kardex kardex;
    private CKardex controllerKardex;
    private CAlmacenProducto controllerAp;
    private boolean esActualizacion = false;
    public PanelAperturaDeInventario(Producto prd) {
        initComponents();
        this.prd = prd;        
        if(prd.getPrimaryKey() != null)
        { 
            tblAlmacencantidades.setModel(new ModeloTablaApertura(this.jlbNota,this.prd));
        }else{
             tblAlmacencantidades.setModel(new ModeloTablaApertura(this.jlbNota));
        }
        Helper.ajustarAnchoColumnas(this.tblAlmacencantidades);
        TableColumn tc = tblAlmacencantidades.getColumnModel().getColumn(1);
        TableCellEditor tce = new CeldaSpinnerEditor();
        tc.setCellEditor(tce);
        this.ftxtCosto.setValue(prd.getCosto());
        this.ftxtFecha.setValue(Calendar.getInstance().getTime());
        kardex = new Kardex();
    }
   

    public Producto getProducto() {
        return prd;
    }

    public void setProducto(Producto prd) {
        this.prd = prd;
    }
    
    private void getOptionPane()
    {
        if(op != null)
        {
            return;
        }
        Component pdr =this.getParent(); 
        while(pdr.getParent() != null)
        {
            if(pdr instanceof JOptionPane)
            {
                op = (JOptionPane)pdr;
                break;
            }            
            pdr = pdr.getParent();
        }
    }

    public boolean isAceptado() {
        return aceptado;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        panelImage1 = new elaprendiz.gui.panel.PanelImage();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtUsuario = new javax.swing.JTextField();
        ftxtFecha = new javax.swing.JFormattedTextField();
        ftxtCosto = new javax.swing.JFormattedTextField();
        jlbNota = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblAlmacencantidades = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        bntGrabar = new elaprendiz.gui.button.ButtonRect();
        bntCancelar = new elaprendiz.gui.button.ButtonRect();
        buttonRect3 = new elaprendiz.gui.button.ButtonRect();

        setLayout(new java.awt.BorderLayout());

        panelImage1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/elaprendiz/resources/fondoazulceleste.jpg"))); // NOI18N
        panelImage1.setLayout(new javax.swing.BoxLayout(panelImage1, javax.swing.BoxLayout.PAGE_AXIS));

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setOpaque(false);
        jPanel1.setLayout(new java.awt.GridBagLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("Responsable:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 1, 4);
        jPanel1.add(jLabel1, gridBagConstraints);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setText("Fecha:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 1, 4);
        jPanel1.add(jLabel2, gridBagConstraints);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel3.setText("Costo Total Por Unidad:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 7, 4);
        jPanel1.add(jLabel3, gridBagConstraints);

        txtUsuario.setEditable(false);
        txtUsuario.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        txtUsuario.setPreferredSize(new java.awt.Dimension(200, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 1, 0);
        jPanel1.add(txtUsuario, gridBagConstraints);

        ftxtFecha.setEditable(false);
        ftxtFecha.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd/MM/yyyy"))));
        ftxtFecha.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        ftxtFecha.setPreferredSize(new java.awt.Dimension(200, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 1, 0);
        jPanel1.add(ftxtFecha, gridBagConstraints);

        ftxtCosto.setEditable(false);
        ftxtCosto.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        ftxtCosto.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        ftxtCosto.setPreferredSize(new java.awt.Dimension(200, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 7, 0);
        jPanel1.add(ftxtCosto, gridBagConstraints);

        jlbNota.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jlbNota.setText("Cantidad Tota de Productos: 0");
        jlbNota.setPreferredSize(new java.awt.Dimension(260, 17));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 7, 0);
        jPanel1.add(jlbNota, gridBagConstraints);

        panelImage1.add(jPanel1);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "INGRESE CANTIDADES SI LO DESEA", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Aharoni", 1, 14))); // NOI18N
        jPanel2.setOpaque(false);
        jPanel2.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setPreferredSize(new java.awt.Dimension(452, 150));

        tblAlmacencantidades.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        tblAlmacencantidades.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Almacen", "Cantidad"
            }
        ));
        tblAlmacencantidades.setRowHeight(22);
        jScrollPane1.setViewportView(tblAlmacencantidades);

        jPanel2.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        panelImage1.add(jPanel2);

        jPanel3.setOpaque(false);

        bntGrabar.setBackground(new java.awt.Color(51, 153, 255));
        bntGrabar.setText("Grabar");
        bntGrabar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntGrabarActionPerformed(evt);
            }
        });
        jPanel3.add(bntGrabar);

        bntCancelar.setBackground(new java.awt.Color(51, 153, 255));
        bntCancelar.setText("Cancelar");
        bntCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntCancelarActionPerformed(evt);
            }
        });
        jPanel3.add(bntCancelar);

        buttonRect3.setBackground(new java.awt.Color(51, 153, 255));
        buttonRect3.setText("buttonRect3");
        jPanel3.add(buttonRect3);

        panelImage1.add(jPanel3);

        add(panelImage1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void bntCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntCancelarActionPerformed
        getOptionPane();
        this.aceptado = false;
        op.setValue(1);
    }//GEN-LAST:event_bntCancelarActionPerformed

    private void bntGrabarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntGrabarActionPerformed
        this.aceptado = true;
        if(!esActualizacion)
        {
            kardex.setProducto(prd);
            kardex.setFecha(Calendar.getInstance().getTime());
            kardex.setDocumento(Kardex.APERTURA);
            kardex.setNumeroDeDocumento("000000");
            kardex.setEntrada(1); 
        }else{
            controllerKardex = new CKardex();
            kardex = (Kardex)controllerKardex.buscarRegistro(new Object[]{Kardex.APERTURA,prd.getPrimaryKey()});
        }
        getOptionPane();
        op.setValue(1);
    }//GEN-LAST:event_bntGrabarActionPerformed

    
    public int grabarDatos()
    {
        ModeloTablaApertura mta = (ModeloTablaApertura)this.tblAlmacencantidades.getModel();
        ArrayList<AlmacenProducto> datos = mta.getDatos();
        controllerAp = new CAlmacenProducto();
        controllerKardex = new CKardex();
        if(kardex != null)
        {
            kardex.setEntrada(mta.getTotalProductos());
            if(!esActualizacion)
            {
                boolean guardarRegistro = controllerKardex.guardarRegistro(kardex); 
            }else{
                controllerKardex.actualizarRegistro(kardex);
            }
            
        }
        
        int actualizarRegistro = 0;
        for(AlmacenProducto ap:datos)
        {
            actualizarRegistro = controllerAp.actualizarRegistro(ap);           
        }
        return actualizarRegistro; 
    }
    
    public void setEsActualizacion()
    {
        this.esActualizacion = true;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private elaprendiz.gui.button.ButtonRect bntCancelar;
    private elaprendiz.gui.button.ButtonRect bntGrabar;
    private elaprendiz.gui.button.ButtonRect buttonRect3;
    private javax.swing.JFormattedTextField ftxtCosto;
    private javax.swing.JFormattedTextField ftxtFecha;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel jlbNota;
    private elaprendiz.gui.panel.PanelImage panelImage1;
    private javax.swing.JTable tblAlmacencantidades;
    private javax.swing.JTextField txtUsuario;
    // End of variables declaration//GEN-END:variables
}
